/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */

package cn.wildfire.chat.kit;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wildfire.chat.kit.contact.UserListAdapter;
import cn.wildfire.chat.kit.contact.model.FooterValue;
import cn.wildfire.chat.kit.contact.model.HeaderValue;
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.contact.viewholder.UserViewHolder;
import cn.wildfire.chat.kit.contact.viewholder.footer.FooterViewHolder;
import cn.wildfire.chat.kit.contact.viewholder.header.HeaderViewHolder;
import cn.wildfire.chat.kit.conversation.ConversationMemberAdapter;
import cn.wildfire.chat.kit.user.UserInfoActivity;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfirechat.model.GroupMember;
import cn.wildfirechat.model.UserInfo;

public class UserListSimpleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //        public UserListSimpleAdapter(FragmentActivity context,List<UIUserInfo> data) {
//        super();
//        this.context = context;
//        this.data = data;
//    }
    private OnMemberClickListener onMemberClickListener;
    private FragmentActivity activity;
    private List<UserInfo> data;
    public UserListSimpleAdapter(FragmentActivity activity){
        this.activity = activity;
    }
    public void setData(List<UserInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }
    public void setOnMemberClickListener(OnMemberClickListener onMemberClickListener) {
        this.onMemberClickListener = onMemberClickListener;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(activity).inflate(R.layout.contact_item_contact, parent, false);
        UserSimpleViewHolder viewHolder = new UserSimpleViewHolder(activity, itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof UserSimpleViewHolder) {
            if(data.size() > 0){
                ((UserSimpleViewHolder) holder).onBind(data.get(position));
            }
        }
    }

    @Override
    public int getItemCount() {
        if (data==null){
            return 0;
        }else {
            return data.size();
        }
    }
    public interface OnMemberClickListener {
        void onUserMemberClick(UserInfo userInfo);
    }
    public class UserSimpleViewHolder extends RecyclerView.ViewHolder {
        protected UserListAdapter adapter;
        @BindView(R2.id.portraitImageView)
        ImageView portraitImageView;
        @BindView(R2.id.nameTextView)
        TextView nameTextView;
        @BindView(R2.id.descTextView)
        TextView descTextView;
        @BindView(R2.id.categoryTextView) TextView categoryTextView;

        protected UserInfo userInfo;

        @OnClick(R2.id.contactLinearLayout)
        void onClick() {
            if (onMemberClickListener == null) {
                return;
            }
            if (userInfo != null) {
                onMemberClickListener.onUserMemberClick(userInfo);
            }
        }
        public UserSimpleViewHolder(FragmentActivity fragmentActivity, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void onBind(UserInfo userInfo) {
            this.userInfo = userInfo;
            categoryTextView.setVisibility(View.GONE);
            nameTextView.setText(userInfo.displayName);
//        GlideApp.with(fragment).load(userInfo.getUserInfo().portrait).placeholder(R.mipmap.avatar_def)
//            .transforms(new CenterCrop(), new RoundedCorners(10))
//            .into(portraitImageView);
        }
    }
}
